/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-06-21     cheng       the first version
 */
#ifndef LIBRARIES_GD32_DRIVERS_DRV_PWM_INPUTCAPTURE_H_
#define LIBRARIES_GD32_DRIVERS_DRV_PWM_INPUTCAPTURE_H_


#include <rtthread.h>
#include "rtdevice.h"
#include <rthw.h>
#include "drivers/PWM_input_capture.h"

#ifdef __cplusplus
extern "C" {
#endif


struct gd32_pwm_inputcapture_device
{
    struct pwm_inputcapture_device pwm_inputcap;
    uint32_t timerx;
    IRQn_Type timerx_irqn;
    uint16_t input_ch;
    uint32_t timer_rcu;
    uint32_t GPIOx;
    uint32_t PINx;
    uint32_t GPIO_rcu;
    uint32_t input_freq_min;
    char *name;
};

#ifdef BSP_USING_PWM_INPUTCAPTURE1
#ifndef PWM_INPUTCAPTURE1_CONFIG
#define PWM_INPUTCAPTURE1_CONFIG                       \
    {                                                  \
       .timerx    = TIMER1,                             \
       .timerx_irqn            = TIMER1_IRQn,           \
       .input_ch               = TIMER_CH_0,            \
       .timer_rcu               = RCU_TIMER1,           \
       .GPIO_rcu                = RCU_GPIOA,            \
       .GPIOx                   = GPIOA,                \
       .PINx                    = GPIO_PIN_0,           \
       .input_freq_min          = 800,                 \
       .name                    = "pwm_inputcap1"       \
    }
#endif /* PWM_INPUTCAPTURE1_CONFIG */
#endif /* BSP_USING_PWM_INPUTCAPTURE1 */

#ifdef BSP_USING_PWM_INPUTCAPTURE2
#ifndef PWM_INPUTCAPTURE2_CONFIG
#define PWM_INPUTCAPTURE2_CONFIG                       \
    {                                                  \
       .timerx    = TIMER2,                             \
       .timerx_irqn            = TIMER2_IRQn,           \
       .input_ch               = TIMER_CH_0,             \
       .timer_rcu               = RCU_TIMER2,           \
       .GPIO_rcu                = RCU_GPIOA,            \
       .GPIOx                   = GPIOA,                \
       .PINx                    = GPIO_PIN_0,           \
       .input_freq_min          = 1000,                 \
       .name                    = "pwm_inputcap2"       \
    }
#endif /* PWM_INPUTCAPTURE2_CONFIG */
#endif /* BSP_USING_PWM_INPUTCAPTURE2 */

#ifdef BSP_USING_PWM_INPUTCAPTURE3
#ifndef PWM_INPUTCAPTURE3_CONFIG
#define PWM_INPUTCAPTURE3_CONFIG                       \
    {                                                  \
       .timerx    = TIMER3,                             \
       .timerx_irqn            = TIMER3_IRQn,           \
       .input_ch               = TIMER_CH_1,             \
       .timer_rcu               = RCU_TIMER3,           \
       .GPIO_rcu                = RCU_GPIOB,            \
       .GPIOx                   = GPIOB,                \
       .PINx                    = GPIO_PIN_7,           \
       .input_freq_min          = 800,                 \
       .name                    = "pwm_inputcap3"       \
    }
#endif /* PWM_INPUTCAPTURE3_CONFIG */
#endif /* BSP_USING_PWM_INPUTCAPTURE3 */

#ifdef BSP_USING_PWM_INPUTCAPTURE4
#ifndef PWM_INPUTCAPTURE4_CONFIG
#define PWM_INPUTCAPTURE4_CONFIG                       \
    {                                                  \
       .timerx    = TIMER4,                             \
       .timerx_irqn            = TIMER4_IRQn,           \
       .input_ch               = TIMER_CH_0,             \
       .timer_rcu               = RCU_TIMER4,           \
       .GPIO_rcu                = RCU_GPIOA,            \
       .GPIOx                   = GPIOA,                \
       .PINx                    = GPIO_PIN_0,           \
       .input_freq_min          = 2000,                 \
       .name                    = "pwm_inputcap4"       \
    }
#endif /* PWM_INPUTCAPTURE4_CONFIG */
#endif /* BSP_USING_PWM_INPUTCAPTURE4 */

#ifdef BSP_USING_PWM_INPUTCAPTURE5
#ifndef PWM_INPUTCAPTURE5_CONFIG
#define PWM_INPUTCAPTURE5_CONFIG                       \
    {                                                  \
       .timerx    = TIMER5,                             \
       .timerx_irqn            = TIMER5_DAC_IRQn,       \
       .input_ch               = TIMER_CH_0,             \
       .timer_rcu               = RCU_TIMER5,           \
       .GPIO_rcu                = RCU_GPIOA,            \
       .GPIOx                   = GPIOA,                \
       .PINx                    = GPIO_PIN_0,           \
       .input_freq_min          = 2000,                 \
       .name                    = "pwm_inputcap5"       \
    }
#endif /* PWM_INPUTCAPTURE5_CONFIG */
#endif /* BSP_USING_PWM_INPUTCAPTURE5 */

rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin);

#ifdef __cplusplus
}
#endif

#endif /* LIBRARIES_GD32_DRIVERS_DRV_PWM_INPUTCAPTURE_H_ */
